#include <glib.h>
#include "Lexicon.h"
#include "LexiconIter.h"
#include "../types/Pair.h"
#include "../types/Regex.h"


Lexicon* lexicon_new(void)
{
  Lexicon* lexicon = (Lexicon*)g_malloc0(sizeof(Lexicon));
  lexicon->tokens  = NULL;
  return lexicon;
}


void lexicon_free(Lexicon* lexicon)
{
  GList* item = lexicon->tokens;
  while (item) {
    Pair* pair = (Pair*)item->data;
    regex_free((Regex*)pair->two);
    g_free(pair);
    item = item->next;
  }
  g_list_free(lexicon->tokens);
  g_free(lexicon);
}


void lexicon_append(Lexicon*    lexicon,
                    const char* name,
                    const char* pattern)
{
  Pair* pair      = (Pair*)g_malloc0(sizeof(Pair));
  pair->one       = (void*)name;
  pair->two       = (void*)regex_new(pattern, 0);
  lexicon->tokens = g_list_append(lexicon->tokens, (gpointer)pair);
}
